﻿define(['admin/app', 'service/admin/tokenTransferService'], function (app) {
    "use strict";
    app.controller('tokenTransferController', function ($scope, $state, $stateParams,  $uibModal,$location, utils, w5cValidator, tokenTransferService, baseService) {
        $scope.list = {};
        $scope.query = { page: 1, size: 20 };
        $scope.query_detail = { page: 1, size: 20 };
        $.extend($scope.query, $stateParams);
        var methods = {
            search: function () {
                if ($state.includes('app.token_transfer.list_pay')) { $scope.query.type = 2; }
                else { $scope.query.type = 1; }
                tokenTransferService.gets($scope.query).success(function (response) { $scope.model = response.data; });
            },
            search_page: function (page) {
                $scope.query.page = page;
                $location.search('page', $scope.query.page);
            },
            edit: function () {
                if ($stateParams.id > 0) {
                    tokenTransferService.get({ id: $stateParams.id, queryPage: $scope.query_detail }).success(function (response) {
                        $scope.model = response.data;
                    });
                  
                }
                $scope.breadcrumb = "详情信息";
            },
            search_transfer_detail_page: function (page) {
                $scope.query_detail.page = page;
                methods.edit('page', $scope.query_detail);
            },
            audit: function () {
                var modal = utils.confirm({ msg:"您确定要保存么", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    var model = $scope.model;
                    $scope.psdisabled = true;
                    tokenTransferService.audit(model.id, model.status, model.remark).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        $scope.psdisabled = false;
                        if (response.result_code == 1000) {
                            methods.goBack();
                        }
                    });
                });
            },
            payoff: function () {
                var modal = utils.confirm({ msg: "您确定要保存么", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    var model = $scope.model;
                    $scope.psdisabled = true;
                    tokenTransferService.payoff(model.id, model.status, model.transaction_no, model.remark).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        $scope.psdisabled = false;
                        if (response.result_code == 1000) {
                            methods.goBack();
                        }
                    });
                });
            },
            transfer_first: function (model) {
                //var model = $scope.model;
                $scope.psdisabled = true;
                tokenTransferService.transfer_first(model.merchant.merchant_no, model.pay_info.order_no).success(function (response) {
                    $scope.psdisabled = false;
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    if (response.result_code == 1000) {
                        methods.goBack();
                    }
                });
            },
            updateBank: function (model) {
                $uibModal.open({
                    templateUrl: 'bank.html',
                    controller: 'bank_ctrl',
                    resolve: {
                        model: function () {
                            return model;
                        }
                    }
                }).result.then(function (model) {
                    methods.search();
                });
            },
            goBack: function () {
                history.back();
            }
        };

        angular.extend($scope, methods);
        if ($state.includes('app.token_transfer.detail') || $state.includes('app.token_transfer.audit') || $state.includes('app.token_transfer.pay')) {
            methods.edit();
        } else {
            methods.search();
        }
    });
    app.controller('bank_ctrl', function ($scope, $uibModalInstance, utils, tokenTransferService, model) {
        $scope.model = model;
        $scope.updateBank = function () {
            $scope.psdisabled = true;
            tokenTransferService.update_bank($scope.model).success(function (response) {
                utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                if (response.result_code == 1000)
                    $uibModalInstance.close($scope.model);
                else
                    $scope.psdisabled = false;
            }).error(function () { $scope.psdisabled = false; });
        };

        $scope.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        };
    });
});